package javaTest;

public class MyLinkedlist implements IList{
//内部类
    static class ListNode{
        public int val;//三个域
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }

    private ListNode head;
    private ListNode last;

    @Override
    public void remove(int key){
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                //开始删除
                cur.prev.next=cur.prev;
                cur.next.prev=cur.next;
                return;
            }else{
                cur=cur.next;
            }
        }
    }

       public void addIndex(int index,int data){
           int len=size();
           if(index<0||index>len){
               return;
           }
           if(index==0){
               addFirst(data);
               return;
           }
           if (index == len) {
               addLast(data);
               return;
           }
            ListNode cur=FindIndex(index);
           System.out.println();
       }



       private ListNode FindIndex(int index){
           ListNode cur=head;
           while(index!=0){
               cur=cur.next;
               index--;
           }
           return cur;
       }
    //
    @Override
         public int size(){
            ListNode cur=head;
            int count=0;
            while(cur!=null){
              count++;
             cur=cur.next;
    }
             return count;
    }


    @Override
    public void display(){
        ListNode cur=head;
        while(cur!=null){
            System.out.println( cur.val+" ");
            cur=cur.next;
        }
        System.out.println(" ");
    }


    @Override
    public boolean contains(int key){
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }

            cur=cur.next;
        }
        return false;
    }

    @Override
    public void addFirst(int data){
        ListNode node=new ListNode(data);
        if(head==null){
            head=last=node;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }

        @Override
        public void addLast(int data){
            ListNode node=new ListNode(data);//构造函数的调用
            if(last==null){
                head=last=node;
            }else{
                last.next=node;
                node.prev=last;
                last=node;
            }
        }
        @Override
           public void clear(){
              ListNode cur=head;
              while(cur!=null){
                  ListNode curN=cur.next;
                  cur.prev=null;
                  cur.next=null;
                  cur=curN;
              }
              head=null;
              last=null;
        }
























}
